package com.qumitech.visualnovel.service.account.impl;

import com.alibaba.fastjson.JSON;
import com.qumitech.visualnovel.component.util.HttpInvokeUtil;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.datasource.app.AccountMapper;
import com.qumitech.visualnovel.model.dao.console.UserInfo;
import com.qumitech.visualnovel.model.common.UrlConstant;
import com.qumitech.visualnovel.pubdef.dto.register.RegisterGotoVoiceRoomCfgDTO;
import com.qumitech.visualnovel.service.account.RegisterService;
import com.qumitech.visualnovel.service.exception.ExceptionService;
import com.qumitech.visualnovel.datasource.console.AccountRegisterMapper;
import com.qumitech.visualnovel.pubdef.dto.register.VirtualAccountDTO;
import com.qumitech.visualnovel.pubdef.dto.register.VirtualAccountInfoDTO;
import com.google.common.collect.ImmutableMap;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @className: RegisterServiceImpl
 * @description:
 * @author: bzhao
 * @date: 2020/11/9 5:02 下午
 * copyright(c) 2017-2020 趣米（武汉）科技有限公司
 */
@Service
public class RegisterServiceImpl implements RegisterService {

    private static final String REGISTER_GOTO_VOICE_ROOM_URL = "/admin/register/goto/voice_room";

    @Autowired
    private ExceptionService exceptionService;

    @Autowired
    private AccountRegisterMapper accountRegisterMapper;

    @Autowired
    private AccountMapper accountMapper;

    @Override
    public BaseRsp listRegisterGotoVoiceRoomCfg(Map<String, String> paramMap) {
        String url = UrlConstant.SERVER_URL + REGISTER_GOTO_VOICE_ROOM_URL;
        String body = HttpInvokeUtil.httpGet(url, paramMap, null);
        BaseRsp baseRsp = JSON.parseObject(body, BaseRsp.class);
        exceptionService.getFailureResultEnum(baseRsp);
        return baseRsp;
    }

    @Override
    public BaseRsp saveRegisterGotoVoiceRoomCfg(RegisterGotoVoiceRoomCfgDTO adminDTO) {
        adminDTO.setOperator(getOperatorName());
        String url = UrlConstant.SERVER_URL + REGISTER_GOTO_VOICE_ROOM_URL;
        String req = JSON.toJSONString(adminDTO);
        String body = HttpInvokeUtil.httpPutJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp baseRsp = JSON.parseObject(body, BaseRsp.class);
        exceptionService.getFailureResultEnum(baseRsp);
        return baseRsp;
    }

    @Override
    public BaseRsp updateRegisterGotoVoiceRoomCfg(RegisterGotoVoiceRoomCfgDTO adminDTO) {
        adminDTO.setOperator(getOperatorName());
        String url = UrlConstant.SERVER_URL + REGISTER_GOTO_VOICE_ROOM_URL;
        String req = JSON.toJSONString(adminDTO);
        String body = HttpInvokeUtil.httpPostJson(url, req, UrlConstant.DefaultEncode, null);
        BaseRsp baseRsp = JSON.parseObject(body, BaseRsp.class);
        exceptionService.getFailureResultEnum(baseRsp);
        return baseRsp;
    }

    @Override
    public BaseRsp deleteRegisterGotoVoiceRoomCfg(Long id) {
        String url = UrlConstant.SERVER_URL + REGISTER_GOTO_VOICE_ROOM_URL + "/" + id;
        String body = HttpInvokeUtil.httpDelete(url, null);
        BaseRsp baseRsp = JSON.parseObject(body, BaseRsp.class);
        exceptionService.getFailureResultEnum(baseRsp);
        return baseRsp;
    }

    private String getOperatorName() {
        Subject subject = SecurityUtils.getSubject();
        UserInfo activeUser = (UserInfo) subject.getPrincipal();
        return activeUser.getUsername();
    }

    @Override
    public BaseRsp listVirtualAccounts(String channel, String startTime, String endTime, int limit, int offset) {
        List<VirtualAccountDTO> listVirtualAccounts = accountRegisterMapper.statisticVirtualAccounts(channel, startTime, endTime, limit, offset);
        if (channel != null && channel.equals("total")) {
            listVirtualAccounts.forEach(x -> x.setChannel("合计"));
        }
        Integer count = accountRegisterMapper.countVirtualAccounts(channel, startTime, endTime);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("data", listVirtualAccounts, "count", count));
    }
    
    @Override
    public BaseRsp listVirtualAccountsDetail(String channel, String targetDate, Integer pageSize, Integer offset) {
        List<Integer> accountID = accountRegisterMapper.listVirtualAccounts(channel, targetDate);
        if (accountID.size()==0) {
            accountID.add(0);
        }
        List<VirtualAccountInfoDTO> virtualAccountsDetail = accountRegisterMapper.getVirtualAccountSDetail(accountID, pageSize, offset);
        Integer virtualAccountsDetailCount = accountRegisterMapper.countVirtualAccountSDetail(accountID);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("data", virtualAccountsDetail, "count", virtualAccountsDetailCount));
    }
    
}
